package com.jml.advice;


import javax.servlet.http.HttpServletRequest;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.servlet.ModelAndView;
import com.jml.vo.ErrorInfo;
@RestControllerAdvice  //作为一个控制层的切面处理使用的是AOP
public class GlobalExceptionHandler {

	private static final String DEFAULT_ERROR_VIEW="error";
	@ExceptionHandler(value = Exception.class)
	public Object  defaultErrorHandler(HttpServletRequest request,Exception e){
		ErrorInfo info=new ErrorInfo();
		info.setCode("100");
		info.setMessage(e.getMessage());
		info.setUrl(request.getRequestURL().toString());
		return info;
	}

	/*//定义错误显示页error.html
	private static final String DEFAULT_ERROR_VIEW="error";
	//所有的异常都是Exception的子类，只要是这个异常就走这个方法
	@ExceptionHandler(value = Exception.class)
	public ModelAndView  defaultErrorHandler(HttpServletRequest request,Exception e){
		//设置跳转路径
		ModelAndView modelAndView=new ModelAndView(DEFAULT_ERROR_VIEW);
		//传递异常对象
		modelAndView.addObject("exception",e);
		//获取请求路径
		modelAndView.addObject("url",request.getRequestURL());
		return modelAndView;
	}*/
}
